<?php

namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\User;
use app\common\model\user\Commission as UserCommission;

/**
 * 收益
 */
class Commission extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];



    /**
     * 收益记录
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="type", type="int", required=true, description="类型:fx=团队,level=等级,dev=差额,channel=渠道,first=一级管理佣金,two=二级管理佣金")
     * @ApiParams (name="page", type="integer", required=true, description="页码")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function list()
    {
        $rej = $this->validate(
            $this->request->post(),
            [
                'type'  => 'require|in:fx,level,dev,channel,first,two',
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $user = $this->auth->getUser();
        if ($this->request->post('type') == 'fx') {
            $res = UserCommission::alias('c')->join('user u', 'c.source_user_id=u.id', 'left')->where('c.user_id', $user->id)->where('c.type', 'in', ['fx', 'only_fx'])->field('c.*,u.nickname')->order('c.id desc')->paginate(10);
        } else {
            $res = UserCommission::alias('c')->join('user u', 'c.source_user_id=u.id', 'left')->where('c.user_id', $user->id)->where('c.type', $this->request->post('type'))->field('c.*,u.nickname')->order('c.id desc')->paginate(10);
        }

        $this->responseMsg(0, $res);
    }

    /**
     * 收益统计
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="type", type="int", required=true, description="类型:fx=团队,level=等级,dev=差额,channel=渠道,first=一级管理佣金,two=二级管理佣金")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function statistics()
    {
        $rej = $this->validate(
            $this->request->post(),
            [
                'type'  => 'require|in:fx,level,dev,channel,first,two',
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $user = $this->auth->getUser();
        if ($this->request->post('type') == 'fx') {
            $today = UserCommission::whereTime('createtime', 'd')->where('user_id', $user->id)->where('type', 'in', ['fx', 'only_fx'])->sum('money');
            $month = UserCommission::whereTime('createtime', 'm')->where('user_id', $user->id)->where('type', 'in', ['fx', 'only_fx'])->sum('money');
            $total = UserCommission::where('user_id', $user->id)->where('type', 'in', ['fx', 'only_fx'])->sum('money');
        } else {
            $today = UserCommission::whereTime('createtime', 'd')->where('user_id', $user->id)->where('type', $this->request->post('type'))->sum('money');
            $month = UserCommission::whereTime('createtime', 'm')->where('user_id', $user->id)->where('type', $this->request->post('type'))->sum('money');
            $total = UserCommission::where('user_id', $user->id)->where('type', $this->request->post('type'))->sum('money');
        }

        $this->responseMsg(0, compact('today', 'month', 'total'));
    }

    /**
     * 收益合计
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function total_income()
    {
        $user = $this->auth->getUser();
        $today = UserCommission::whereTime('createtime', 'd')->where('user_id', $user->id)->sum('money');
        $month = UserCommission::whereTime('createtime', 'm')->where('user_id', $user->id)->sum('money');
        $total = UserCommission::where('user_id', $user->id)->sum('money');
        $this->responseMsg(0, compact('today', 'month', 'total'));
    }
}
